package internationalenergytradingplatform.controller.AdministratorsController;

import internationalenergytradingplatform.VO.AdminHomeEchart;
import internationalenergytradingplatform.VO.AdministratorLoginVO;
import internationalenergytradingplatform.constant.MessageConstant;
import internationalenergytradingplatform.dao.AdministratorAccountDao;
import internationalenergytradingplatform.dao.BuyerAccountDao;
import internationalenergytradingplatform.dao.SupplierAccountDao;
import internationalenergytradingplatform.dao.TransporterAccountMapper;
import internationalenergytradingplatform.exception.AccountNotFoundException;
import internationalenergytradingplatform.exception.PasswordErrorException;
import internationalenergytradingplatform.pojo.AdministratorAccount;
import internationalenergytradingplatform.pojo.BuyerAccount;
import internationalenergytradingplatform.pojo.SupplierAccount;
import internationalenergytradingplatform.pojo.TransporterAccount;
import internationalenergytradingplatform.properties.JwtProperties;
import internationalenergytradingplatform.result.Result;
import internationalenergytradingplatform.utils.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 名称AdministratorAccountController
 * 描述
 *
 * @version 1.0
 * @author:DuanMM
 * @datatime:2024-03-28 16:38
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/administrator-account")

public class AdministratorAccountController {

    @Autowired
    private AdministratorAccountDao administratorAccountDao;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    SupplierAccountDao supplierAccountDao;

    @Autowired
    BuyerAccountDao buyerAccountDao;

    @Autowired
    TransporterAccountMapper transporterAccountMapper;

    /**
     * 管理员登录
     */
    @PostMapping("/login")
    public Result<AdministratorLoginVO> login(@RequestBody AdministratorAccount administratorAccount){
        String tele = administratorAccount.getTele();
        String password = administratorAccount.getPassword();
        AdministratorAccount administratorByTele = administratorAccountDao.getAdministratorByTele(tele);
        //如果账号不存在
        if(administratorByTele == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //对密码md5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //密码错误
        if (!password.equals(administratorByTele.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("tele", administratorByTele.getTele());
        String token = JWTUtil.createJWT(
                jwtProperties.getAdminSecretKey(),//令牌的key
                jwtProperties.getAdminTtl(),//令牌的有效时间
                claims);

        //将管理员信息写入VO中
        AdministratorLoginVO administratorLoginVO = AdministratorLoginVO.builder()
                .tele(administratorByTele.getTele())
                .name(administratorByTele.getName())
                .password(administratorByTele.getPassword())
                .token(token)
                .build();
        log.info("登录结果：{}", administratorLoginVO);
        return Result.success(administratorLoginVO);
    }

    /**
     *
     * @author yingfeng
     * @date 2024/4/3 23:14
    返回首页Echarts信息
     */
    @GetMapping("/getEchartsInfo")
    public Result<AdminHomeEchart> getEchartsInfo(){
        //初始化AdminHomeEchart对象
        AdminHomeEchart adminHomeEchart = new AdminHomeEchart();
        //获取供应商数量
        List<SupplierAccount> allSupplier = supplierAccountDao.getAllSupplier();
        adminHomeEchart.setSupplierNumber(allSupplier.size());
        //获取采购商数量
        List<BuyerAccount> allBuyerAccount = buyerAccountDao.getAllBuyerAccount();
        adminHomeEchart.setBuyerNumber(allBuyerAccount.size());
        //获取运输商的数量
        List<TransporterAccount> allTransporterAccount = transporterAccountMapper.getAllTransporterAccount();
        adminHomeEchart.setTransporterNumber(allTransporterAccount.size());
        return Result.success(adminHomeEchart);
    }
}
